package com.lbd.base.controller;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by WangYajie on 2016/10/12.
 */
@Controller
@RequestMapping("/util")
public class UtilController {
    /**
     * 汉字转拼音
     *
     * @param text 汉字
     * @return 拼音
     * @throws BadHanyuPinyinOutputFormatCombination 汉字转拼音异常
     */
    @ResponseBody
    @RequestMapping("/pinyin")
    public String pinyin(String text) throws BadHanyuPinyinOutputFormatCombination {
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        return PinyinHelper.toHanYuPinyinString(text, defaultFormat, "", true);
    }
}
